from configparser import ConfigParser
from pathlib import Path
from typing import Optional, Any

class ConfigManager:
    _instance = None

    def __new__(cls, config_path: str = "config.ini"):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, config_path: str = "config.ini"):
        if not hasattr(self, 'config'):
            self.config = ConfigParser()
            self.config_path = Path(config_path)
            if self.config_path.exists():
                self.config.read(config_path)
            else:
                raise FileNotFoundError(f"Config file not found: {config_path}")

    def get(self, key: str, section: str = "DEFAULT") -> Optional[str]:
        """Get configuration value by key and section."""
        try:
            return self.config.get(section, key)
        except Exception as e:
            return None

    def get_index_dir(self) -> str:
        """Get index directory path."""
        return self.get("index_dir")

    def get_base_url(self) -> str:
        """Get base URL."""
        return self.get("base_url")

    def get_model(self) -> str:
        """Get model name."""
        return self.get("model")

    def get_api_key(self) -> str:
        """Get API key."""
        return self.get("api_key")
